<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="common/head::head">
</head>

<body>
<div class="layui-container">
    <form method="post" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">Token</label>
            <div class="layui-input-block">
                <input type="text" name="token" id="token" readonly
                       th:value="${session.user?.getUserMd5Key()}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" id="email"
                       placeholder="请输入邮箱" th:value="${session.user?.email}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="randCode">
            <div class="layui-inline">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input type="number" placeholder="请输入验证码" name="code" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal" id="getCode">获取验证码</a>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">钉钉</label>
            <div class="layui-input-block">
                <input type="text" name="dingDing" id="dingDing" th:value="${session.user?.dingDing}"
                       placeholder="请输入群webhook" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业微信</label>
            <div class="layui-input-block">
                <input type="text" name="workWx" id="workWx" th:value="${session.user?.workWx}"
                       placeholder="请输入群webhook" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/javascript">
    asyncFn.push(function () {
        var randCode = $("#randCode");
        if ("[[${session.user?.email}]]" !== "") {
            randCode.hide();
        }

        $("input[name='email']").change(function () {
            var val = $(this).val();
            if (val === "[[${session.user?.email}]]") {
                randCode.hide();
            } else {
                randCode.show();
            }
        });
        $("#getCode").click(function () {
            var that = $(this);
            if (that.hasClass('layui-disabled')) {
                return;
            }
            var val = $("input[name='email']").val();
            if (!val || val === '') {
                layer.msg("请输入邮箱");
                return false;
            }
            loadingAjax({
                url: './sendCode.json',
                data: {
                    email: val
                },
                success: function (data) {
                    layer.msg(data.msg);
                    if (200 === data.code) {
                        that.addClass("layui-disabled");
                        var time = 60;
                        var timeInter = setInterval(function () {
                            time--;
                            that.text(time + " S");
                            if (time === 0) {
                                clearInterval(timeInter);
                                that.text("获取验证码");
                                that.removeClass("layui-disabled");
                            }
                        }, 1000);
                    }
                }
            });
        });

        // 提交用户信息
        form.on('submit(submit)', function (data) {
            loadingAjax({
                url: './save_basicInfo.json',
                data: data.field,
                success: function (data) {
                    layer.msg(data.msg);
                    if (200 === data.code) {
                        autoClose();
                    }
                }
            });
            return false;
        });
    })
</script>
</html>